package com.apes.fico.engine.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fico.masterdata.gl.model.AccountingVoucherType;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: apestech
 * @description: 财务凭证
 * @author: Chengxm
 * @create: 2018-12-24 13:53
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TempVoucher.CONST_TABLE_NAME)
public class TempVoucher extends Model {
    public final static String CONST_TABLE_NAME = "gl_voucher_temp";

    @Id
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "财务凭证", padding = 8)
    @Comment("凭证内码")
    private String id;

    @NotNull
    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "post_date")
    @Comment("记账日期")
    private Date postDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "acc_voucher_type_id", referencedColumnName = "id", nullable = false)
    @Comment("凭证类型")
    private AccountingVoucherType accountingVoucherType;

    @OneToMany(mappedBy = "voucher", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<TempVoucherItem> voucherItemList = new ArrayList(); //凭证分录

    public List<TempVoucherItem> getVoucherItemList() {
        for (TempVoucherItem item : voucherItemList) {
            item.setVoucher(this);
        }
        return voucherItemList;
    }

    @Column(name = "debit_total", columnDefinition = ApesConst.FIELD_MONEY_16_4_0, nullable = false)
    @Comment("借方合计")
    private double debitTotal = 0;

    @Column(name = "credit_total", columnDefinition = ApesConst.FIELD_MONEY_16_4_0, nullable = false)
    @Comment("贷方合计")
    private double creditTotal = 0;

    @Name
    @Column(name = "remark", length = 200)
    @Comment("备注")
    private String remark;

    @Column(name = "attachment", columnDefinition = ApesConst.FIELD_QTY_16_4)
    @Comment("附件数")
    private int attachment;

    @Column(name = "state",columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("已归集标记")
    private Boolean state;

    @Column(name = "business_type", length = 64, nullable = false)
    @Comment("业务凭证类型")
    private String businessType;

    @Column(name = "business_id", length = 32, nullable = false)
    @Comment("业务凭证编号")
    private String businessId;

    @Column(name = "collect_key", length = 32, nullable = false)
    @Comment("归集key")
    private String collectKey;

    @Lob
    @JSONField(serialize = false)
    @Basic(fetch = FetchType.LAZY)
    @Comment("归集值")
    @Column(name = "collect_value", columnDefinition = "CLOB")
    private String collectValue;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "voucher_template_id", referencedColumnName = "id", nullable = false)
    @Comment("模板类型")
    private VoucherTemplate voucherTemplate;

}
